package com.example.service;

import ch.qos.logback.core.util.StringUtil;
import cn.hutool.core.util.StrUtil;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import com.fasterxml.jackson.core.util.InternalJacksonUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public List<User> selectAll(User user) {
        return userMapper.selectAll(user);

    }

    public User login(User user) {
        String username = user.getUsername();
        User match_user = userMapper.selectByUsername(username);
        if (match_user == null) {
            throw new CustomException("用户名不存在", 500);
        } else {
            String password = match_user.getPassword();
            if (!password.equals(user.getPassword())) {
                throw new CustomException("账号或密码错误", 500);
            }
        }
        return match_user;
    }

    public void register(User user) {
        String username = user.getUsername();
        User match_user = userMapper.selectByUsername(username);
        if (match_user != null) {
            throw new CustomException("用户名已存在", 500);
        } else {
            user.setAvatar("");
            user.setSex("");
            user.setAge(0);
            userMapper.addUser(user);
        }
    }

    public void update(User user) {
        userMapper.updateById(user);
    }
}
